package com.todo.ln.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.todo.ln.entity.Admin;
import com.todo.ln.mapper.AdminMapper;
import com.todo.ln.service.AdminService;
import com.todo.ln.utils.RestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 1.@description:
 * 2.@author:ln
 * 3.@Date:2022/9/15
 */
@Service //表示当前是service层的注解，将AdminServiceImpl交给Spring容器管理
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    // 实现登陆功能
    public RestUtils login(String username, String password) {
        // 查询数据，查询前端传递的数据是否与数据库中的数据一致
        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("username",username));
        // 校验数据
        // 判断当前用户是否存在
        if(admin==null){
            return new RestUtils(500,"用户名不存在",null);
        }
        // 判断当前用户的密码是否正确
        if(!password.equals(admin.getPassword())){
            return new RestUtils(500,"密码错误",null);
        }else{
            return new RestUtils(200,"success login",admin);
        }
    }

    @Override
    //查所有
    public RestUtils adminList() {

        return new RestUtils(200,"",adminMapper.selectList(null));
    }
    @Override
    // 模糊查询
    public RestUtils adminListByLikeUsername(String username){
        return new RestUtils(200,"",adminMapper.selectList(new QueryWrapper<Admin>().like("username",username)));
    }

    // 分页查询
    @Override
    public RestUtils adminListByPage(Integer current, Integer rows) {
        // 1.第一步分页查询数据
        Page<Admin> adminPage = adminMapper.selectPage(new Page<Admin>(current,rows),null);
        // 2.存储返回的数据----HashMap
        HashMap<String,Object> map = new HashMap<>();
        // 3.存储当前页的所有数据
        map.put("admins",adminPage.getRecords());
        // 4.存储总页数
        map.put("pages",adminPage.getPages());
        // 5.存储总条数
        map.put("total",adminPage.getTotal());
        // 返回RestUtils结果集
        return new RestUtils(200,"分页查询成功！",map);

    }

    @Override
    // 增加用户
    public RestUtils addAdmin(Admin admin) {
        // 返回结果集
        return adminMapper.insert(admin) >0?
                new RestUtils(200,"添加成功！",null) : new RestUtils(500,"添加失败！",null);
    }

    @Override
    // 修改用户信息
    public RestUtils updateAdmin(Admin admin) {
        return adminMapper.updateById(admin) >0? new RestUtils(200,"修改成功！",null):new RestUtils(500,"修改失败！",null);

    }

    @Override
    // 逻辑删除用户
    public  RestUtils deleteAdmin(Integer adminId){
        return adminMapper.delete(new QueryWrapper<Admin>().eq("admin_id",adminId)) > 0?
                new RestUtils(200,"删除成功！",null):new RestUtils(500,"删除失败！",null);
    }
    public RestUtils delAdmin(Admin admin) {
        return adminMapper.deleteById(admin) >0 ?new RestUtils(200,"删除成功！",null) : new RestUtils(500,"删除失败！",null);
    }


}

